﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;
using ECGadmin.WebUI.Models;

namespace ECGadmin.WebUI.CustomHelpers
{
    public static class CustomHtmlHelper
    {
        /// <summary>
        /// Create toolbox has edit, delete, add, search buttons
        /// </summary>
        /// <param name="html"></param>
        /// <param name="buttonList"></param>
        /// <returns></returns>
        public static MvcHtmlString ToolBox(this HtmlHelper html, List<object> buttonList)
        {
            if (buttonList != null && buttonList.Count > 0)
            {
                var ultag = new TagBuilder("ul");
                foreach (var item in buttonList)
                {
                    // Add button
                    if (item is Add)
                    {
                        var itemAdd = (Add)item;
                        var liTag = new TagBuilder("li");
                        liTag.MergeAttribute("class", itemAdd.ClassCss);

                        var aTag = new TagBuilder("a");
                        aTag.MergeAttribute("href", itemAdd.ActionName);
                        aTag.InnerHtml = "Thêm mới";
                        aTag.InnerHtml += "<span></span>";

                        liTag.InnerHtml += aTag.ToString();

                        ultag.InnerHtml += liTag.ToString();
                    }

                    // Edit button
                    if (item is Edit)
                    {
                        var itemAdd = (Edit)item;
                        var liTag = new TagBuilder("li");
                        liTag.MergeAttribute("class", itemAdd.ClassCss);

                        var aTag = new TagBuilder("a");
                        aTag.MergeAttribute("href", itemAdd.ActionName);
                        aTag.InnerHtml = "Lưu lại";
                        aTag.InnerHtml += "<span></span>";

                        liTag.InnerHtml += aTag.ToString();

                        ultag.InnerHtml += liTag.ToString();
                    }

                    // Delete button
                    if (item is Delete)
                    {
                        var itemDelete = (Delete)item;
                        var liTag = new TagBuilder("li");
                        liTag.MergeAttribute("class", itemDelete.ClassCss);

                        var aTag = new TagBuilder("a");
                        aTag.MergeAttribute("href", itemDelete.ActionName);
                        aTag.InnerHtml = "Xóa";
                        aTag.InnerHtml += "<span></span>";

                        liTag.InnerHtml += aTag.ToString();

                        ultag.InnerHtml += liTag.ToString();
                    }

                    // Refresh button
                    if (item is Refresh)
                    {
                        var itemRefresh = (Refresh)item;
                        var liTag = new TagBuilder("li");
                        liTag.MergeAttribute("class", itemRefresh.ClassCss);

                        var aTag = new TagBuilder("a");
                        aTag.MergeAttribute("href", itemRefresh.ActionName);
                        aTag.InnerHtml = "Refresh";
                        aTag.InnerHtml += "<span></span>";

                        liTag.InnerHtml += aTag.ToString();

                        ultag.InnerHtml += liTag.ToString();
                    }

                    // Save button
                    if (item is Save)
                    {
                        var itemSave = (Save)item;
                        var liTag = new TagBuilder("li");
                        liTag.MergeAttribute("class", itemSave.ClassCss);

                        var aTag = new TagBuilder("a");
                        aTag.MergeAttribute("href", itemSave.ActionName);
                        aTag.InnerHtml = "Lưu lại";
                        aTag.InnerHtml += "<span></span>";

                        liTag.InnerHtml += aTag.ToString();

                        ultag.InnerHtml += liTag.ToString();
                    }
                    // Back button
                    if (item is Back)
                    {
                        var itemBack = (Back)item;
                        var liTag = new TagBuilder("li");
                        liTag.MergeAttribute("class", itemBack.ClassCss);

                        var aTag = new TagBuilder("a");
                        aTag.MergeAttribute("href", itemBack.ActionName);
                        aTag.InnerHtml = "Danh sách";
                        aTag.InnerHtml += "<span></span>";

                        liTag.InnerHtml += aTag.ToString();

                        ultag.InnerHtml += liTag.ToString();
                    }
                    // List button
                    if (item is ItemList)
                    {
                        var itemList = (ItemList)item;
                        var liTag = new TagBuilder("li");
                        liTag.MergeAttribute("class", itemList.ClassCss);

                        var aTag = new TagBuilder("a");
                        aTag.MergeAttribute("href", itemList.ActionName);
                        aTag.InnerHtml = "Danh sách";
                        aTag.InnerHtml += "<span></span>";

                        liTag.InnerHtml += aTag.ToString();

                        ultag.InnerHtml += liTag.ToString();
                    }
                }
                return MvcHtmlString.Create("<div class='toolBox'><div id='Toolbox'>" + ultag.ToString() + "</div></div>");
            }
            return MvcHtmlString.Create("");
        }

        public static MvcHtmlString PageLinks(this HtmlHelper html, PageInfo pageInfo, Func<int, string> pageUrl)
        {
            if (pageInfo.TotalPages > 0)
            {
                StringBuilder result = new StringBuilder();
                if (pageInfo.TotalPages > 1 && pageInfo.CurrentPage > 1)
                {
                    //Tạo link về đầu
                    TagBuilder FirstTag = new TagBuilder("a");
                    FirstTag.MergeAttribute("href", pageUrl(1));
                    FirstTag.MergeAttribute("class", "first_paging");
                    FirstTag.InnerHtml = "Đầu";
                    result.AppendLine(FirstTag.ToString());
                }
                if (pageInfo.CurrentPage > 1)
                {
                    //Tạo link Trước nếu currentPage > 1
                    TagBuilder tag = new TagBuilder("a");
                    tag.MergeAttribute("href", pageUrl(pageInfo.CurrentPage - 1));
                    tag.MergeAttribute("class", "previous_paging");
                    tag.InnerHtml = "Sau";
                    result.AppendLine(tag.ToString());
                }
                for (int i = 1; i <= pageInfo.TotalPages; i++)
                {
                    TagBuilder tag = new TagBuilder("a");
                    tag.MergeAttribute("href", pageUrl(i));
                    tag.InnerHtml = i.ToString();
                    tag.AddCssClass("number_paging");
                    if (i == pageInfo.CurrentPage)
                        tag.AddCssClass("current_page");
                    result.AppendLine(tag.ToString());
                }
                if (pageInfo.CurrentPage < pageInfo.TotalPages)
                {
                    //Tạo link Sau nếu currentPage < TotalPages

                    TagBuilder tag = new TagBuilder("a");
                    tag.MergeAttribute("href", pageUrl(pageInfo.CurrentPage + 1));
                    tag.MergeAttribute("class", "next_paging");
                    tag.InnerHtml = "Tiếp";
                    result.AppendLine(tag.ToString());
                }

                if (pageInfo.TotalPages > 1 && pageInfo.CurrentPage < pageInfo.TotalPages)
                {
                    //Tạo link về cuối
                    TagBuilder LastTag = new TagBuilder("a");
                    LastTag.MergeAttribute("href", pageUrl(pageInfo.TotalPages));
                    LastTag.MergeAttribute("class", "last_paging");
                    LastTag.InnerHtml = "Cuối";
                    result.AppendLine(LastTag.ToString());
                }
                result.AppendLine(" (" + pageInfo.TotalPages + " Trang)");
                return MvcHtmlString.Create(result.ToString());

            }
            else
                return MvcHtmlString.Create("");
        }

        public static MvcHtmlString SelectAvtive(this HtmlHelper html, bool active, string name)
        {
            var result = new StringBuilder();
            if (active)
            {
                result.AppendFormat("<select name={0}>", name);
            }
            else
            {
                result.AppendFormat("<select name={0} class='selectCommentActive'>", name);
            }
            if(active)
            {
                result.Append("<option value='1' selected='selected'>Kích hoạt</option>");
                result.Append("<option value='0'>Chưa kích hoạt</option>");

            }
            else
            {
                result.Append("<option value='0' selected='selected'>Chưa kích hoạt</option>");
                result.Append("<option value='1'>Kích hoạt</option>");
            }

            result.AppendFormat("</select>");
            return MvcHtmlString.Create(result.ToString());
        }
    }
}